﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace SerializationHelper
{
    public sealed class SerOperate : SerTool, ISerializationObjectHelper
    {
        public SerOperate(string path)
        {
            if (string.IsNullOrEmpty(path))
                throw new ArgumentNullException("path IsNullOrEmpty");
            _path = path;
        }
        #region ISerializationObjectHelper 成员
        public void SerializationObject<T>(T t, SerType serializationType)
        {
            switch (serializationType)
            {
                case SerType.Binary:
                    SerializationObject<T>(t, new BinaryFormatter());
                    break;
                case SerType.SOAP:
                    SerializationObject<T>(t, new SoapFormatter());
                    break;
                case SerType.XML:
                    SerializationObjectToXML<T>(t);
                    break;
            }
        }
        public T DeSerializationObject<T>(SerType serializationType) where T : class
        {
            T t = null;
            switch (serializationType)
            {
                case SerType.Binary:
                    t = DeSerializationObject<T>(new BinaryFormatter());
                    break;
                case SerType.SOAP:
                    t = DeSerializationObject<T>(new SoapFormatter());
                    break;
                case SerType.XML:
                    t = DeSerializationXMLToObject<T>();
                    break;
            }
            return t;
        }
        #endregion
    }
}
